package com.zhoug.zmui3.round;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import com.zhoug.zmui3.R;
import com.zhoug.zmui3.alpha.ZMUIAlphaTextView;
import com.zhoug.zmui3.utils.ZMUIViewHelper;

/**
 * 圆形按钮
 * 需要在主题中定义zmui_style_ZMUIRoundButton或者使用R.style.zmui_style_ZMUIRoundButton样式才会生效
 * @Author 35574
 * @Date 2020/7/24
 * @Description
 */
public class ZMUIRoundButton extends ZMUIAlphaTextView {
    private static final String TAG = ">>>ZMUIRoundButton";
    private static  boolean debug = false;

    private ZMUIViewHelper _mRoundViewHelper;

    public static void setDebug(boolean debug) {
        ZMUIRoundButton.debug = debug;
    }

    public ZMUIRoundButton(Context context) {
        this(context, null);
    }

    public ZMUIRoundButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIRoundButton);
    }

    public ZMUIRoundButton(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, R.style.zmui_style_ZMUIRoundButton);
    }

    public ZMUIRoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        _getZMUIViewHelper().parseAttribute(context, attrs, defStyleAttr, defStyleRes);
    }

    private ZMUIViewHelper _getZMUIViewHelper(){
        if(_mRoundViewHelper==null){
            _mRoundViewHelper = new ZMUIViewHelper(this);
        }
        return _mRoundViewHelper;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        _getZMUIViewHelper().onSizeChanged(w, h);
    }


    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        _getZMUIViewHelper().onPressedChanged(this,pressed);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        _getZMUIViewHelper().onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int size = _getZMUIViewHelper().measureSize(getMeasuredWidth(), getMeasuredHeight());
        if (size > 0) {
            setMeasuredDimension(size, size);
        }
    }

}
